package Presentacion;

import ModeloLogicoSistema.AdministradorCliente;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import ModeloLogicoSistema.Validacion;
/**
 *
 * @author debo
 */
public class AltaCliente extends javax.swing.JDialog {

    AdministradorCliente ac = new AdministradorCliente();
    private boolean registroCompleto=false;

    /**
     * Creates new form AltaCliente
     */
    public AltaCliente(java.awt.Frame parent, boolean modal) throws SQLException {

        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        txtPreCel.setText("15");
        Integer id = ac.nuevoCodigoCliente();
        txtID.setText(id.toString());
        desactivarLabelsError();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtApellido = new javax.swing.JTextField();
        txtPreCel = new javax.swing.JTextField();
        txtLocalidad = new javax.swing.JTextField();
        txtCalle = new javax.swing.JTextField();
        txtNumero = new javax.swing.JTextField();
        txtCP = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        BtnLimpiar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        txtCel = new javax.swing.JTextField();
        txtTel = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtPiso = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        jvalidacionApellido = new javax.swing.JLabel();
        jvalidacionTelefono = new javax.swing.JLabel();
        jvalidacionCalle = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jvalidacionNombre = new javax.swing.JLabel();
        jvalidacionNumero = new javax.swing.JLabel();
        jvalidacionCelular = new javax.swing.JLabel();
        jvalidacionLocalidad = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Alta de Cliente");
        setMinimumSize(new java.awt.Dimension(330, 520));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("ID Cliente");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        jLabel2.setText("Nombre");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 50, -1, -1));

        jLabel3.setText("Apellido");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, -1, -1));

        jLabel4.setText("Teléfono");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 140, -1, -1));

        jLabel5.setText("Celular");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 190, -1, -1));

        jLabel6.setText("Localidad");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 240, -1, -1));

        jLabel7.setText("Calle");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 290, -1, -1));

        jLabel8.setText("Número");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 340, -1, -1));

        jLabel9.setText("C.P");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 390, -1, -1));

        txtID.setEditable(false);
        txtID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIDActionPerformed(evt);
            }
        });
        getContentPane().add(txtID, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 10, 150, -1));

        txtApellido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtApellidoActionPerformed(evt);
            }
        });
        getContentPane().add(txtApellido, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 95, 150, -1));

        txtPreCel.setEditable(false);
        txtPreCel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPreCelActionPerformed(evt);
            }
        });
        getContentPane().add(txtPreCel, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 185, 40, -1));
        getContentPane().add(txtLocalidad, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 235, 150, -1));
        getContentPane().add(txtCalle, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 285, 150, -1));
        getContentPane().add(txtNumero, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 335, 150, -1));

        txtCP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCPActionPerformed(evt);
            }
        });
        getContentPane().add(txtCP, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 385, 150, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Guardar.png"))); // NOI18N
        jButton1.setText("Registrar");
        jButton1.setPreferredSize(new java.awt.Dimension(80, 23));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 463, 100, 30));

        BtnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/limpiar.png"))); // NOI18N
        BtnLimpiar.setText("Limpiar");
        BtnLimpiar.setPreferredSize(new java.awt.Dimension(80, 23));
        BtnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnLimpiarActionPerformed(evt);
            }
        });
        getContentPane().add(BtnLimpiar, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 463, 100, 30));

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/cancelar.png"))); // NOI18N
        jButton3.setText("Cerrar");
        jButton3.setMaximumSize(new java.awt.Dimension(80, 23));
        jButton3.setPreferredSize(new java.awt.Dimension(80, 23));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 463, 100, 30));
        getContentPane().add(txtCel, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 185, 104, -1));

        txtTel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelActionPerformed(evt);
            }
        });
        getContentPane().add(txtTel, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 140, 150, -1));

        jLabel10.setText("Piso Dpto");
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 430, -1, -1));

        txtPiso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPisoActionPerformed(evt);
            }
        });
        getContentPane().add(txtPiso, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 425, 150, -1));

        txtNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreActionPerformed(evt);
            }
        });
        getContentPane().add(txtNombre, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 50, 150, -1));

        jvalidacionApellido.setForeground(new java.awt.Color(204, 0, 0));
        jvalidacionApellido.setText("Este campo no permite números");
        getContentPane().add(jvalidacionApellido, new org.netbeans.lib.awtextra.AbsoluteConstraints(98, 120, 190, -1));

        jvalidacionTelefono.setForeground(new java.awt.Color(204, 0, 0));
        jvalidacionTelefono.setText("Este campo no permite letras ");
        getContentPane().add(jvalidacionTelefono, new org.netbeans.lib.awtextra.AbsoluteConstraints(98, 165, 170, -1));

        jvalidacionCalle.setForeground(new java.awt.Color(204, 0, 0));
        jvalidacionCalle.setText("Este campo no permite números");
        getContentPane().add(jvalidacionCalle, new org.netbeans.lib.awtextra.AbsoluteConstraints(98, 310, 190, -1));

        jLabel21.setForeground(new java.awt.Color(204, 0, 0));
        getContentPane().add(jLabel21, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 280, 120, -1));

        jvalidacionNombre.setForeground(new java.awt.Color(204, 0, 0));
        jvalidacionNombre.setText("Este campo no permite números");
        getContentPane().add(jvalidacionNombre, new org.netbeans.lib.awtextra.AbsoluteConstraints(98, 75, 190, -1));

        jvalidacionNumero.setForeground(new java.awt.Color(204, 0, 0));
        jvalidacionNumero.setText("Este campo no permite letras");
        getContentPane().add(jvalidacionNumero, new org.netbeans.lib.awtextra.AbsoluteConstraints(98, 360, 180, -1));

        jvalidacionCelular.setForeground(new java.awt.Color(204, 0, 0));
        jvalidacionCelular.setText("Este campo no permite letras");
        getContentPane().add(jvalidacionCelular, new org.netbeans.lib.awtextra.AbsoluteConstraints(98, 210, 180, -1));

        jvalidacionLocalidad.setForeground(new java.awt.Color(204, 0, 0));
        jvalidacionLocalidad.setText("Este campo no permite números");
        getContentPane().add(jvalidacionLocalidad, new org.netbeans.lib.awtextra.AbsoluteConstraints(98, 260, 190, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void desactivarLabelsError(){
        
        this.jvalidacionApellido.setVisible(false);
        this.jvalidacionCalle.setVisible(false);
        this.jvalidacionCelular.setVisible(false);
        this.jvalidacionLocalidad.setVisible(false);
        this.jvalidacionNombre.setVisible(false);
        this.jvalidacionNumero.setVisible(false);
        this.jvalidacionTelefono.setVisible(false);
    }
    
    private void activarLabelsError(){
        
        this.jvalidacionApellido.setVisible(true);
        this.jvalidacionCalle.setVisible(true);
        this.jvalidacionCelular.setVisible(true);
        this.jvalidacionLocalidad.setVisible(true);
        this.jvalidacionNombre.setVisible(true);
        this.jvalidacionNumero.setVisible(true);
        this.jvalidacionTelefono.setVisible(true);
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //Registrar
        
        String nombre = Validacion.quitarEspaciosInnecesarios(txtNombre.getText());
        String apellido = Validacion.quitarEspaciosInnecesarios(txtApellido.getText());
        if(Validacion.palabraSinNumero(nombre)== false){
            //this.jvalidacionNombre.setVisible(true);
            this.activarLabelsError();
            return;
        }
        
        if(Validacion.palabraSinNumero(apellido)== false){
            //this.jvalidacionApellido.setVisible(true);
            this.activarLabelsError();
            return;
        }
        if(nombre.isEmpty() | apellido.isEmpty()){
            JOptionPane.showMessageDialog(null, "Los campos nombre y apellido son obligatorios");
            return;
        }
        
        String telefono = Validacion.quitarEspaciosInnecesarios(txtTel.getText());
        String celular = Validacion.quitarEspaciosInnecesarios(txtCel.getText());
        String localidad = Validacion.quitarEspaciosInnecesarios(txtLocalidad.getText());
        String calle = Validacion.quitarEspaciosInnecesarios(txtCalle.getText());
        String numero = Validacion.quitarEspaciosInnecesarios(txtNumero.getText());
        String piso = Validacion.quitarEspaciosInnecesarios(txtPiso.getText());
        String cp = Validacion.quitarEspaciosInnecesarios(txtCP.getText());
        
        if(!Validacion.numeroSinLetras(telefono)){
            //this.jvalidacionTelefono.setVisible(true);
            this.activarLabelsError();
            return;
        }        
        if(!Validacion.numeroSinLetras(celular)){
            this.activarLabelsError();
            return;
        }       
        if(!Validacion.palabraSinNumero(localidad)){
            this.activarLabelsError();
            return;
        }        
        if(!Validacion.palabraSinNumero(calle)){
            this.activarLabelsError();
            return;
        }        
        if(!Validacion.numeroSinLetras(numero)){
            this.activarLabelsError();
            return;
        }
        AdministradorCliente admCliente = null;
        try {
            admCliente = new AdministradorCliente();
        } catch (SQLException ex) {
            Logger.getLogger(AltaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            admCliente.persistirCliente(nombre, apellido, calle, numero, localidad, cp, piso, telefono, celular);
        } catch (SQLException ex) {
            Logger.getLogger(AltaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.registroCompleto=true;
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed
    
    boolean estaRegistrado(){
        return registroCompleto;
    }
    
    private void BtnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnLimpiarActionPerformed
        // Cancelar
        //txtID.setText(null);
        txtApellido.setText(null);
        //txtPreCel.setText(null);
        txtLocalidad.setText(null);
        txtPiso.setText(null);
        txtNombre.setText(null);
        txtCP.setText(null);
        txtNumero.setText(null);
        txtCalle.setText(null);
        txtTel.setText(null);
        txtCel.setText(null);
        this.desactivarLabelsError();
    }//GEN-LAST:event_BtnLimpiarActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // Cerrar
        this.setVisible(false);
        BuscarCliente buscarCliente = new BuscarCliente(null, rootPaneCheckingEnabled);
        buscarCliente.setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void txtIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIDActionPerformed

    private void txtApellidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtApellidoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtApellidoActionPerformed

    private void txtTelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTelActionPerformed

    private void txtCPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCPActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCPActionPerformed

    private void txtPisoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPisoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPisoActionPerformed

    private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreActionPerformed
        this.jvalidacionNombre.setVisible(false);
    }//GEN-LAST:event_txtNombreActionPerformed

    private void txtPreCelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPreCelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPreCelActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(AltaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(AltaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(AltaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(AltaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                AltaCliente dialog = null;
//                try {
//                    dialog = new AltaCliente(new javax.swing.JFrame(), true);
//                } catch (SQLException ex) {
//                    Logger.getLogger(AltaCliente.class.getName()).log(Level.SEVERE, null, ex);
//                }
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnLimpiar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jvalidacionApellido;
    private javax.swing.JLabel jvalidacionCalle;
    private javax.swing.JLabel jvalidacionCelular;
    private javax.swing.JLabel jvalidacionLocalidad;
    private javax.swing.JLabel jvalidacionNombre;
    private javax.swing.JLabel jvalidacionNumero;
    private javax.swing.JLabel jvalidacionTelefono;
    private javax.swing.JTextField txtApellido;
    private javax.swing.JTextField txtCP;
    private javax.swing.JTextField txtCalle;
    private javax.swing.JTextField txtCel;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtLocalidad;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNumero;
    private javax.swing.JTextField txtPiso;
    private javax.swing.JTextField txtPreCel;
    private javax.swing.JTextField txtTel;
    // End of variables declaration//GEN-END:variables
}
